package com.upcode.weighing.oauthserver.config;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class MyPasswordEncoder extends BCryptPasswordEncoder{

	@Override
	public String encode(CharSequence rawPassword) {
		CharSequence pwd = decryptPwd(rawPassword);
		return super.encode(pwd);
	}

	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		CharSequence pwd = decryptPwd(rawPassword);
		return super.matches(pwd, encodedPassword);
	}

	
	private CharSequence decryptPwd(CharSequence rawPassword) {
		CharSequence password = rawPassword;

		
		/*boolean needDecrypt = false;
	        try {
	        	Base64.getDecoder().decode(rawPassword.toString());
	        	needDecrypt = true;
	        }catch (Exception e) {	
			}	
	        if(needDecrypt) {
	        	try {
	        		System.out.println("======================================================");
	        		System.out.println("encpy pwd: ");
	        		System.out.println("======================================================");
	     			password = PassWordUtils.decrypt(rawPassword.toString(), pwdPrivateKey);
	     			System.out.println("org pwd : ");
	     		} catch (Exception e) {
	     			String msg = e.getMessage();
	    			System.out.println(msg);
	     			throw new RuntimeException("密码解密失败");
	     		}
	        }*/	
		
		return password;
	}

	public static void main(String[] args) {
		String pwd="User@123";
		MyPasswordEncoder encoder=new MyPasswordEncoder();
		String enPwd=encoder.encode(pwd);
		System.out.println(enPwd);
	}

}
